package xin.nick.demo;

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicIntegerTest {

    private static final int THREADS_COUNT = 20;
    public static AtomicInteger count = new AtomicInteger(0);

    public static void main(String[] args) {
//        Thread.currentThread().getThreadGroup().list();
//        System.out.println(Thread.activeCount());
        Thread[] threads = new Thread[THREADS_COUNT];

        for (int i = 0; i < THREADS_COUNT; i++) {
            threads[i] = new Thread() {
                @Override
                public void run() {
                    for(int i = 0; i < 1000; i++) {
                        count.incrementAndGet();
                    }
                }
            };
            threads[i].start();
        }

        // idea 默认会启动两条线程
        while (Thread.activeCount() > 2) {
            Thread.yield();
        }

        System.out.println(count);
    }

}
